package icursoft.educationms.util;

import android.content.Context;
import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import icursoft.educationms.bean.StudentInfoBO;
import icursoft.educationms.http.HttpCallBack;
import icursoft.educationms.http.HttpResult;
import icursoft.educationms.servive.PreferencesService;
import okhttp3.Response;

/**
 * Created by wang on 2017/4/19.
 */

public class MachTalkUtil {
    public static String TEST_URL = "http://www.iwesong.com:8016/tools/";
    public static String OKGO_URL = "http://www.iwesong.com:8016/tools/student.ashx?action=";
    public static String BASE_URL = TEST_URL;
    public static SimpleDateFormat dateFormatter = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss", Locale.CHINA);

    public static String TAG = MachTalkUtil.class.getSimpleName();
    private static MachTalkUtil util;

    public static String token;

    public static MachTalkUtil getInstance(Context context) {
        token = PreferencesService.getInstance(context).getTokenShort();
        if (util == null) {
            util = new MachTalkUtil();
        }
        return util;
    }

    /**
     * 用户登录
     *
     * @param loginCallback
     * @param signinName
     * @param password
     */
    public static void userLogin(Context context, StringCallback loginCallback, String signinName, String password) {

//        Map<String, Object> params = new HashMap<>();
//      //  params.put("action", "login");
//        params.put("user_name", signinName);
//        params.put("password", password);
        OkGo.post(OKGO_URL + "login")//
                .tag(context)//
                .params("user_name", signinName)
                .params("password", CommUtil.getMD5(password))
                .execute(loginCallback);
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.userPost(params);
//        httpResultCall.enqueue(loginCallback);
    }

    /**
     * 忘记密码
     *
     * @param resetCallBack
     * @param phone
     * @param password
     */
    public static void resetPassWord(Context context, StringCallback resetCallBack, String phone, String password) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("user_name", phone);
//        params.put("password", password);
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.resetPassWord(params);
//        httpResultCall.enqueue(resetCallBack);
        OkGo.post(OKGO_URL + "find_pwd")//
                .tag(context)//
                .params("user_name", phone)
                .params("password", CommUtil.getMD5(password))
                .execute(resetCallBack);
    }

    /**
     * 1：代表手机号无效或请求参数格式错误；
     * 2：代表注册时手机号已存在；
     * 3：代表重复发送之前仍有效；
     * 4：服务器接口问题；
     * 5：发送超过最大量
     * 6:找回验证码手机号不存在或账号被禁用
     **/
    public static void sendValidationCode(HttpCallBack<HttpResult> callback, String phone, String type) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("phone", phone);
//        params.put("type", type);
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.sendValidationCode(params);
//        httpResultCall.enqueue(callback);
    }

    /**
     * 注册
     *
     * @param addUserCallback
     * @param phone
     * @param password
     */
    public static void addUser(Context context, StringCallback addUserCallback, String phone, String password) {

//        Map<String, Object> params = new HashMap<>();
//        // params.put("action", "reg_user");
//        params.put("user_name", phone);
//        params.put("password", password);//CommUtil.getMD5(password)
        OkGo.post(OKGO_URL + "reg_user")//
                .tag(context)//
                .params("user_name", phone)
                .params("password", CommUtil.getMD5(password))
                .execute(addUserCallback);
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.addUser(params);
//        httpResultCall.enqueue(addUserCallback);
    }

    /**
     * 修改密码
     *
     * @param updatePwdcallback
     * @param user_name
     * @param oldPwd
     * @param newPwd
     */
    public static void updateDoctorPwd(Context context, StringCallback updatePwdcallback, String user_name, String oldPwd, String newPwd, String token) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("doctorId", doctorId);
//        params.put("oldPwd", oldPwd);
//        params.put("newPwd", newPwd);
//
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.updateDoctorPwd(token, params);
//        httpResultCall.enqueue(callback);
        OkGo.post(OKGO_URL + "update_pwd")//
                .tag(context)//
                .params("user_name", user_name)
                .params("oldpwd", oldPwd)
                .params("newpwd", newPwd)
                .params("token", token)
                .execute(updatePwdcallback);
    }
    /**
     * 获取信息
     *
     * @param getSelfInfocallback
     * @param user_name
     */
    public void getSelfInfo(Context context, StringCallback getSelfInfocallback, String user_name, String  token) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("doctorId", doctorId);
//        params.put("oldPwd", oldPwd);
//        params.put("newPwd", newPwd);
//
//        RetrofitImpl clientRetrofit = RetrofitClient.getInstance().getApiService();
//        Call<HttpResult> httpResultCall = clientRetrofit.updateDoctorPwd(token, params);
//        httpResultCall.enqueue(callback);
        OkGo.post(OKGO_URL + "get_self_info")//
                .tag(context)//
                .params("user_name", user_name)
                .params("token", token)
                .execute(getSelfInfocallback);
    }
    /**
     * 编辑完善学生信息
     *
     * @param editcallback
     * @param info
     */
    public  void editStudentInfo(Context context, StringCallback editcallback, StudentInfoBO info) {

//        Map<String, Object> params = new HashMap<>();
//        params.put("doctorId", info.getDoctorId());
//        params.put("name", info.getName());
//        if (!TextUtils.isEmpty(info.getAvatar()))
//            params.put("avatar", info.getAvatar());
//        if (!TextUtils.isEmpty(info.getIdNum()))
//            params.put("idNum", info.getIdNum());
//        if (!TextUtils.isEmpty(info.getPpqc()))
//            params.put("ppqc", info.getPpqc());
//        if (!TextUtils.isEmpty(info.getPqc()))
//            params.put("pqc", info.getPqc());
//        params.put("hospital", info.getHospital());
//        params.put("section", info.getSection());
//        params.put("jobTitle", info.getJobTitle());
//        if (!TextUtils.isEmpty(info.getPrimaryField()))
//            params.put("primaryField", info.getPrimaryField());
        try {
            new String(info.getReal_name().toString().getBytes("UTF-8"));
        OkGo.post(OKGO_URL + "update_self_info")//
                .tag(context)//
                .params("user_name", info.getUser_name())
                .params("token", PreferencesService.getInstance(context).getTokenLong())
                .params("real_name",info.getReal_name() )
                .params("phone", info.getPhone())
                .params("pro_phone", info.getPro_phone())
                .params("email",  info.getEmail())
                .params("sex", info.getSex())
                .params("identifier", info.getIdentifier())
                .params("address", info.getAddress())
                .params("qq", info.getQq())
                .params("school", info.getSchool())
                .params("major", info.getMajor())
                .params("enter_year", info.getEnter_year())
                .params("nation", info.getNation())
                .params("province", info.getProvince())
                .params("is_art", info.getIs_art())
                .params("english_level", info.getEnglish_level())
                .execute(editcallback);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
